package eteam.aps.service.impl;

import eteam.aps.common.component.LoginUserRedisService;
import eteam.aps.common.entity.ParentEntity;
import eteam.aps.common.helper.Helper;
import eteam.aps.common.helper.JsonPage;
import eteam.aps.mapper.DataDictionaryMapper;
import eteam.aps.model.entity.DataDictionary;
import eteam.aps.model.vo.DataDictionaryVo;
import eteam.aps.service.DataDictionaryService;
import eteam.aps.systeminterface.model.dto.VerifyTokenDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据字典业务层
 * 
 * @author yiyikai
 */
@Service
public class DataDictionaryServiceImpl implements DataDictionaryService {

	@Autowired
	private DataDictionaryMapper dataDictionaryMapper;

	@Autowired
	private LoginUserRedisService loginUserRedisService;


	@Transactional
	@Override
	public int insert(DataDictionary dictionary) throws Exception{
		VerifyTokenDto user = loginUserRedisService.getUser();
		dictionary.setfId(Helper.GetId());
		dictionary.setfState(ParentEntity.FSTATE_DRAFT);
		dictionary.setfCreateUserId(user.getFuserid());
		dictionary.setfDdcManagerId(user.getFuserid());
		LocalDateTime ldt = LocalDateTime.now();
		dictionary.setfCreatorTime(ldt);
		dictionary.setfLastModifyTime(ldt);
		dictionary.setfBookCode(user.getFgrouporgid());
		return dataDictionaryMapper.insert(dictionary);
	}

	@Transactional
	@Override
	public int delete(String fId) {
		return dataDictionaryMapper.deleteByFId(fId);
	}

	@Transactional
	@Override
	public int update(DataDictionary dictionary) throws Exception{
		VerifyTokenDto user = loginUserRedisService.getUser();
		dictionary.setfLastModifyUserId(user.getFuserid());
		dictionary.setfLastModifyTime(LocalDateTime.now());
		return dataDictionaryMapper.updateOne(dictionary);
	}

	@Override
	public JsonPage<DataDictionaryVo> selectAll(Map<String, Object> map, int page, int pageSize) {
		map.put("fState",ParentEntity.FSTATE_DELETED);
		int totalCount = dataDictionaryMapper.selectCountAll(map);
		List<DataDictionaryVo> list = null;
		if (totalCount > 0) {
			map.put("offset", (page - 1) * pageSize);
			map.put("limit", pageSize);
			list = dataDictionaryMapper.selectAll(map);
		}
		return JsonPage.builder(page, pageSize, totalCount, list);
	}

	@Override
	public int selectCountByFDdcId(String fDdcId) {
		Map<String, Object> map = new HashMap<String, Object>(2);
		map.put("fDdcId", fDdcId);
		map.put("fState", ParentEntity.FSTATE_DELETED);
		return dataDictionaryMapper.selectCountByFDdcId(map);
	}

	@Override
	public DataDictionaryVo selectById(String fId) {
		return dataDictionaryMapper.selectByfId(fId);
	}

	@Override
	public int selectCountByFDdCode(String fDdCode) {
		return dataDictionaryMapper.selectCountByFDdCode(fDdCode);
	}

	@Override
	public int selectCountByFDdName(String fDdName) {
		return dataDictionaryMapper.selectCountByFDdName(fDdName);
	}

	@Override
	public DataDictionaryVo selectByfId(String fId) {
		return dataDictionaryMapper.selectByfId(fId);
	}

	@Override
	public int selectFStateByFId(String fId) {
		return dataDictionaryMapper.selectFStateByFId(fId);
	}
}
